// pages/my/myindex.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userinfo: {
      rolename: '客户',
      accounts: '0.00'
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        var imghgt = 363 * res.windowWidth / 750;
        that.setData({
          top_img_hgt: imghgt
        });
      },
    });
    this.loadToServer();
  },
  loadToServer: function () {
    var url = app.globalData.webUrl + 'User/userinfo';
    app.loadDataFromServer(url, this.loadCallBack, {});
  },
  loadCallBack: function (res) {

    wx.hideToast();
    if (res.status != 'ok') {
      wx.showToast({
        title: '网络错误~',
        image: '../../imgs/tips.png'
      });
      return;
    }
    var userinfo = res.result;
    if (userinfo.role == null || userinfo.role == undefined){
      return;
    }
    userinfo.rolename = app.getUserRole(userinfo.role);
    userinfo.sexname = userinfo.sex == 1 ? '男' : '女';
    userinfo.birthdaystr = app.formatDateTime(userinfo.birthday, false);
    app.globalData.userinfo = userinfo;
    this.setData({
      userinfo: userinfo
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.userinfo){
      this.setData({
        userinfo: app.globalData.userinfo
      });
      console.log('aa1');
    }
    console.log('aa');
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  onscanCodeEnd: function (res) {
    console.log(res)
    //扫码结束
    if (res == '' || res == null || res == undefined) {
      console.log('参数错误.');
      return;
    }
    var arrs = res.split(":");
    if (arrs.length < 1) {
      console.log('参数错误..');
      return;
    }

    var tpstr = arrs[0];
    switch (tpstr) {
      case 'use':
        this.useToServer(arrs[1], arrs[2], arrs[3]);
        break;
    }

  },
  useToServer: function (cuid, buyid, userid) {
    console.log('cuid=' + cuid + 'buyid=' + buyid + 'userid=' + userid);
    var url = app.globalData.webUrl + 'Goods/useCoupon';
    var tpdata = { cuid: cuid, buyid: buyid, userid: userid};
    app.loadDataFromServer(url, this.UseCallBack, tpdata);
  },
  UseCallBack: function (res) {

    wx.hideToast();
    if (res.status != 'ok') {
      wx.showToast({
        title: res.msg
      });
      return;
    }

    wx.showToast({
      title: res.msg
    });
  },
  menuSelect: function (e) {
    var mtype = e.currentTarget.dataset.mtype;
    if (mtype == 'info') {
      wx.navigateTo({
        url: './info/info',
      });
    }
    else if (mtype == 'qr') {
      wx.navigateTo({
        url: './qrcode/qrcode',
      });
    }
    else if (mtype == 'date') {
      wx.navigateTo({
        url: './mydate/mydate',
      });
    }
    else if (mtype == 'money') {
      wx.navigateTo({
        url: './getmoney/money',
      });
    }
    else if (mtype == 'addpro') {
      wx.navigateTo({
        url: './addpro/addpro',
      });
    }
    else if (mtype == 'client') {
      wx.navigateTo({
        url: './clients/clients',
      });
    }
    else if (mtype == 'inout') {
      wx.navigateTo({
        url: './inout/inout',
      });
    }
    else if (mtype == 'scan') {
      wx.scanCode({
        success: (res) => {
          console.log(res);
          this.onscanCodeEnd(res.result);
        }
      })
    }
    else if (mtype == 'order') {
      wx.navigateTo({
        url: './order/order',
      });
    }
  }
})